/*
 * Copyright 2014 Jens K., Jalbum AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.package net.jalbum.jprint;
 */
package net.jalbum.areatagging;

import com.adobe.xmp.XMPException;
import com.adobe.xmp.XMPMeta;
import com.adobe.xmp.options.PropertyOptions;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import static net.jalbum.areatagging.AreaTagHandler.MWG_RS_NS;

/**
 * An instance of a single area tag circle (x + y + diameter d).
 */
public class AreaTagCircle extends AreaTag {

    private float d;

    public AreaTagCircle(String name, String description, String type, float x, float y, float d) {
        setName(name);
        setDescription(description);
        setType(type);
        setX(x);
        setY(y);
        this.d = d;
    }

    public float getD() {
        return d;
    }

    public void setD(float d) {
        this.d = d;
    }

    public void setWeight(float xWeight) {
        float min = 0.0f + (d / 2.0f);
        float max = 1.0f - (d / 2.0f);
        setX(capValue(xWeight, min, max));
    }

    @Override
    void drawInactive(Graphics2D big, Dimension sDim) {
        // Not implemented
    }

    @Override
    void drawActive(Graphics2D big, Dimension sDim, String name) {
        // Not implemented
    }

    @Override
    int resizeOrMove(MouseState state, Dimension sDim, Point p) {
        // Not implemented
        return Cursor.DEFAULT_CURSOR;
    }

    @Override
    MouseState updateMouseState(Graphics2D g, Dimension sDim, Point p) {
        // Not implemented
        return MouseState.OUTSIDE;
    }

    @Override
    boolean isPointInsideTag(Graphics2D g, Dimension dim, Point p) {
        // Not implemented
        return false;
    }

    @Override
    boolean isPointInsideName(Graphics2D g, Dimension dim, Point p) {
        // Not implemented
        return false;
    }

    @Override
    void storeMWGAreaTags(XMPMeta meta, int item) throws XMPException {
        // convert and format tag information
        DecimalFormat f = new DecimalFormat("#0.000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
        double xF = (double) getX();
        xF = Double.parseDouble(f.format(xF));
        double yF = (double) getY();
        yF = Double.parseDouble(f.format(yF));
        double dF = (double) d;
        dF = Double.parseDouble(f.format(dF));

        String name = getName();
        String description = getDescription();
        String type = getType();

        // create array node
        meta.appendArrayItem(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList", new PropertyOptions().setArray(true), "", new PropertyOptions().setStruct(true));

        // store area information
        meta.setProperty(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Area/stArea:unit", getUnit());
        meta.setPropertyDouble(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Area/stArea:x", xF);
        meta.setPropertyDouble(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Area/stArea:y", yF);
        meta.setPropertyDouble(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Area/stArea:w", dF);

        // store additional information (if there are some)
        if ((name != null && !name.isEmpty())) {
            meta.setProperty(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Name", name);
        }
        if ((description != null && !description.isEmpty())) {
            meta.setProperty(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Description", description);
        }
        if ((type != null)) {
            meta.setProperty(MWG_RS_NS, "mwg-rs:Regions/mwg-rs:RegionList[" + item + "]/mwg-rs:Type", type);
        }

        item++;
    }

    @Override
    void storeMSAreaTags(XMPMeta meta, int item, Dimension dim) throws XMPException {
        // Not needed because MS-Meta can't handle circle tags
    }

}
